package com.demo.jsf;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import javax.faces.context.FacesContext;

import com.demo.javabean.DBAccess;

public class SmsAction extends BaseAction {

	private String id;
	private String username;
	private String sender;
	private String message;
	private String sendtime;
	private String isRead;

	public String list() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";

		// 取得分页参数
		String strPageSize = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageSize");
		if (strPageSize != null) {
			pageSize = Integer.parseInt(strPageSize);
		}
		String strPageNo = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("pageNo");
		if (strPageNo != null) {
			pageNo = Integer.parseInt(strPageNo);
		}

		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {

			// 查询总的记录数，计算跳页参数
			int start = pageSize * (pageNo - 1);
			// 总记录数查询SQL
			String sql1 = "select count(*) as countall from sms where username='"
					+ loginAction.getUsername() + "'";
			db.query(sql1);
			if (db.next()) {
				// 计算总行数并保存
				String countall = db.getValue("countall");
				rowCount = Integer.parseInt(countall);

				// 计算总页数并保存
				pageCount = rowCount % pageSize == 0 ? rowCount / pageSize
						: rowCount / pageSize + 1;

				// 计算跳页参数并保存
				pageFirstNo = 1;// 首页
				pageLastNo = pageCount;// 尾页
				pagePreNo = pageNo > 1 ? pageNo - 1 : 1;// 前一页
				pageNextNo = pageNo < pageCount ? pageNo + 1 : pageCount;// 后一页
			}
			db.closeRs();

			// 取得当前页数据SQL
			String sql2 = "select * from sms where username='" + username
			+ "' order by sendtime desc limit " + start + ","
			+ pageSize;
			list = new ArrayList<Hashtable<String, String>>();
			db.query(sql2);
			while (db.next()) {
				// 查询每行数据的各个字段数据
				Hashtable<String, String> hash = new Hashtable<String, String>();
				hash.put("id", db.getValue("id"));
				hash.put("username", db.getValue("username"));
				hash.put("sender", db.getValue("sender"));
				hash.put("message", db.getValue("message"));
				hash.put("sendtime", db.getValue("sendtime"));
				hash.put("isRead", db.getValue("isRead"));

				// 保存当前行
				list.add(hash);
			}
			db.closeRs();
			db.closeStm();
			db.closeConn();
		}

		return "smslist";
	}

	public String add() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		return "smsadd";
	}

	public String insert() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			SimpleDateFormat format = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
			sendtime = format.format(new Date());

			// 组合新增SQL
			String sql = "insert into sms (username, sender, message, sendtime, isRead) ";
			sql += " values('" + username + "','" + loginAction.getUsername() + "','" + message
					+ "','" + sendtime + "','0')";

			// 转换参数编码
			try {
				sql = new String(sql.getBytes("ISO8859-1"), "UTF-8");
			} catch (Exception e) {
			}

			// 执行插入
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String read() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "addresslist";
		}
		
		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 组合更新SQL
			String sql = "update sms set isRead='1' where id=" + id;

			// 执行更新
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String delete() {
		// 身份验证
		if (loginAction.getUsername() == null)
			return "login";
		
		// 取得参数
		id = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get("id");
		if (id == null) {
			return "smslist";
		}
		
		// 创建数据库连接
		DBAccess db = new DBAccess();
		if (db.createConn()) {
			// 根据id组成删除SQL，执行删除
			String sql = "delete from sms where id=" + id;
			db.update(sql);
			db.closeStm();
			db.closeConn();
		}
		return list();
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getSendtime() {
		return sendtime;
	}

	public void setSendtime(String sendtime) {
		this.sendtime = sendtime;
	}

	public String getIsRead() {
		return isRead;
	}

	public void setIsRead(String isRead) {
		this.isRead = isRead;
	}
}